package com.school.campus.service.impl;

import java.util.Collections;
import java.util.List;

import com.school.campus.domain.Activity;
import com.school.campus.domain.vo.ActivityRegistrationVO;
import com.school.campus.mapper.ActivityMapper;
import com.school.common.utils.DateUtils;
import com.school.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.school.campus.mapper.ActivityRegistrationMapper;
import com.school.campus.domain.ActivityRegistration;
import com.school.campus.service.IActivityRegistrationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 活动报名Service业务层处理
 *
 * @author wwj
 * @date 2025-11-07
 */
@Service
public class ActivityRegistrationServiceImpl implements IActivityRegistrationService {
    @Autowired
    private ActivityRegistrationMapper activityRegistrationMapper;
    @Autowired
    private ActivityMapper activityMapper;

    /**
     * 查询活动报名
     *
     * @param id 活动报名主键
     * @return 活动报名
     */
    @Override
    public ActivityRegistration selectActivityRegistrationById(Long id) {
        return activityRegistrationMapper.selectActivityRegistrationById(id);
    }

    /**
     * 查询活动报名列表
     *
     * @param activityRegistration 活动报名
     * @return 活动报名
     */
    @Override
    public List<ActivityRegistration> selectActivityRegistrationList(ActivityRegistration activityRegistration) {
        return activityRegistrationMapper.selectActivityRegistrationList(activityRegistration);
    }

    /**
     * 新增活动报名
     *
     * @param activityRegistration 活动报名
     * @return 结果
     */
    @Override
    @Transactional
    public int insertActivityRegistration(ActivityRegistration activityRegistration) {
        try {
            Activity activity = activityMapper.selectActivityById(activityRegistration.getActivityId());
            if (activity.getCurrentSignUpNum() == activity.getSignUpLimit()) {
                throw new RuntimeException("该活动报名人数已满!");
            }

            activityRegistration.setUserId(SecurityUtils.getUserId());

            activityRegistration.setRegistrationTime(DateUtils.getNowDate());
            int i = activityRegistrationMapper.insertActivityRegistration(activityRegistration);

            activity.setCurrentSignUpNum(activity.getCurrentSignUpNum() + 1);

            activityMapper.updateActivity(activity);
            return i;
        } catch (Exception e) {
            if (e.getMessage().contains("Duplicate entry")) {
                throw new RuntimeException("您已经报名过该活动，不可重复报名!");
            }
            throw new RuntimeException("报名失败，请联系管理员!");
        }
    }

    /**
     * 修改活动报名
     *
     * @param activityRegistration 活动报名
     * @return 结果
     */
    @Override
    public int updateActivityRegistration(ActivityRegistration activityRegistration) {
        return activityRegistrationMapper.updateActivityRegistration(activityRegistration);
    }

    /**
     * 批量删除活动报名
     *
     * @param ids 需要删除的活动报名主键
     * @return 结果
     */
    @Override
    public int deleteActivityRegistrationByIds(Long[] ids) {
        return activityRegistrationMapper.deleteActivityRegistrationByIds(ids);
    }

    /**
     * 删除活动报名信息
     *
     * @param id 活动报名主键
     * @return 结果
     */
    @Override
    public int deleteActivityRegistrationById(Long id) {
        return activityRegistrationMapper.deleteActivityRegistrationById(id);
    }

    @Override
    public List<ActivityRegistrationVO> selectMyActivityRegistrationList(Long userId) {

        return activityRegistrationMapper.selectMyActivityRegistrationList(userId);
    }
}
